﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace B121 {
    public partial class MainWindowForm : Form {

        #region Properties
        private string ProfilesPath { get; set; }
        public List<string[]> Profiles { get; private set; }

        public List<ItemClass> Items { get; private set; }
        public List<ContainerClass> Containers { get; private set; }
        #endregion

        public MainWindowForm() {
            InitializeComponent();
            Items = new List<ItemClass>();
            Containers = new List<ContainerClass>();
        }

        private void addItemBtn_Click(object sender, EventArgs e) {
            AddNewItemForm addItem = new AddNewItemForm();
            if (addItem.ShowDialog() == System.Windows.Forms.DialogResult.OK) {
                foreach (ItemClass item in addItem.Items) {
                    Items.Add(item);
                }

                AccessData.WriteAllItems(ProfilesPath, Items);
                itemListView.Items.Clear();
                ReadItems();
                EnableButtons(true);
            }
        }

        private void newToolStripMenuItem_Click(object sender, EventArgs e) {
            AddNewProfileForm addProfile = new AddNewProfileForm();
            if (addProfile.ShowDialog() == DialogResult.OK) {
                ProfilesPath = Application.StartupPath + "/Profiles";
                Profiles = AccessData.ReadAllProfiles(ProfilesPath);

                profileDropDwnCmbBox.Items.Clear();
                LoadProfileIntoDropdown();
            }
        }

        private void addContainerBtn_Click(object sender, EventArgs e) {
            AddNewContainerForm addContainer = new AddNewContainerForm();
            if (addContainer.ShowDialog() == System.Windows.Forms.DialogResult.OK) {
                Containers.Add(addContainer.AddedContainer);
                AccessData.WriteAllContainers(ProfilesPath, Containers);
                containerListView.Items.Clear();
                ReadContainers();
                EnableButtons(true);
            }
        }

        private void packBagBtn_Click(object sender, EventArgs e) {
            PackingOutputForm packBag = new PackingOutputForm(Items, Containers);
            packBag.ShowDialog();
            ReadContainers();
        }

        private void MainWindowForm_Load(object sender, EventArgs e) {
            ProfilesPath = Application.StartupPath + "/Profiles";

            ProfilesPath = Application.StartupPath + "/Profiles";
            Profiles = AccessData.ReadAllProfiles(ProfilesPath);
            LoadProfileIntoDropdown();

            EnableButtons(false);
        }

        private void EnableButtons(bool enable) {
            addItemBtn.Enabled = enable;
            addContainerBtn.Enabled = enable;

            packBagBtn.Enabled = (enable && Items.Count > 0 && Containers.Count > 0 ? true : false);
            packBagBtn.BackColor = (enable && Items.Count > 0 && Containers.Count > 0 ? Color.LightGreen : Color.Red);
        }

        private void LoadProfileIntoDropdown(string profile = "") {
            profileDropDwnCmbBox.Items.Clear();

            foreach (string[] str in Profiles) { // Add all Profiles
                profileDropDwnCmbBox.Items.Add(str[0]);
            }

            if (profile != "") {

            }
        }

        private void ReadContainers() {
            List<ContainerClass> containers;
            bool didSucceed = AccessData.ReadAllContainersFromProfile(ProfilesPath, out containers); // check retval
            Containers = containers;

            containerListView.Items.Clear();

            foreach (ContainerClass cont in Containers) {
                //MessageBox.Show(cont.ToString());
                ListViewItem lvt = new ListViewItem();
                lvt.Text = cont.Name;
                lvt.SubItems.Add(cont.X.ToString());
                lvt.SubItems.Add(cont.Y.ToString());
                lvt.SubItems.Add(cont.Z.ToString());
                lvt.SubItems.Add(cont.MaxWeight.ToString());
                containerListView.Items.Add(lvt);
            }
        }

        private void ReadItems() {
            List<ItemClass> items;
            bool didSucceed = AccessData.ReadAllItemsFromProfile(ProfilesPath, out items); // check retval
            Items = items;

            itemListView.CheckBoxes = true;
            foreach (ItemClass item in Items) {
                ListViewItem lvt = new ListViewItem();
                lvt.Text = "";
                lvt.SubItems.Add(item.Name);
                lvt.SubItems.Add(item.X.ToString());
                lvt.SubItems.Add(item.Y.ToString());
                lvt.SubItems.Add(item.Z.ToString());
                lvt.SubItems.Add(item.Weight.ToString());

                itemListView.Items.Add(lvt);
            }
        }

        private void profileDropDwnCmbBox_SelectedIndexChanged(object sender, EventArgs e) {
            ProfilesPath = Application.StartupPath + "/Profiles/" + profileDropDwnCmbBox.SelectedItem;
            itemListView.Items.Clear();
            containerListView.Items.Clear();

            ReadContainers();
            ReadItems();

            EnableButtons(profileDropDwnCmbBox.Text == "" ? false : true);
            deleteProfileToolStripMenuItem.Enabled = true;

            deleteContainerBtn.Enabled = false;
            deleteItemBtn.Enabled = false;
        }

        private void deleteProfileToolStripMenuItem_click(object sender, EventArgs e) {
            deleteProfileToolStripMenuItem.Enabled = false;

            ProfilesPath = Application.StartupPath + "/Profiles";
            string[] files = Directory.GetFiles(Application.StartupPath + "/Profiles/" + profileDropDwnCmbBox.SelectedItem);

            foreach (string file in files) { //Delete all files in the folder, before the folder is deleted
                File.Delete(file);
            }

            Directory.Delete(Application.StartupPath + "/Profiles/" + profileDropDwnCmbBox.SelectedItem);
            Profiles = AccessData.ReadAllProfiles(ProfilesPath); // Read the profile again, with one profile less

            profileDropDwnCmbBox.Items.Remove(profileDropDwnCmbBox.SelectedItem); //Delete the selected item
            profileDropDwnCmbBox.Items.Clear(); //Clear dropdown box
            itemListView.Items.Clear(); //Clear item list view
            containerListView.Items.Clear(); //Clear container list view

            LoadProfileIntoDropdown(); //Load items into dropdown
            profileDropDwnCmbBox.SelectedItem = 0; //Select item 0 (blank item)
            profileDropDwnCmbBox.Text = ""; //Clear the selected item in the dropdown list
            EnableButtons(false);
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e) {
            Application.Exit();
        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e) {
            AboutBox aboutOpen = new AboutBox();
            aboutOpen.ShowDialog();
        }

        private void deleteItemBtn_Click(object sender, EventArgs e) {
            foreach (ListViewItem lvtItem in itemListView.SelectedItems) {
                ItemClass itemToDelete = new ItemClass(
                    lvtItem.SubItems[1].Text, // name
                    AccessData.TryToParseInt(lvtItem.SubItems[2].Text), // x
                    AccessData.TryToParseInt(lvtItem.SubItems[3].Text), // y
                    AccessData.TryToParseInt(lvtItem.SubItems[4].Text), // z
                    AccessData.TryToParseInt(lvtItem.SubItems[5].Text) // weight
                    );

                // This might delete the wrong item, but I don't know how to go about it...
                foreach (ItemClass item in Items) {
                    if (item.Name == itemToDelete.Name &&
                        item.X == itemToDelete.X &&
                        item.Y == itemToDelete.Y &&
                        item.Z == itemToDelete.Z &&
                        item.Weight == itemToDelete.Weight) {
                        Items.Remove(item);
                        break;
                    }
                }
            }

            AccessData.WriteAllItems(ProfilesPath, Items);
            profileDropDwnCmbBox_SelectedIndexChanged(this, null);
        }

        private void deleteContainerBtn_Click(object sender, EventArgs e) {
            foreach (ListViewItem lvtItem in containerListView.SelectedItems) {
                ContainerClass containerToDelete = new ContainerClass(
                    lvtItem.SubItems[0].Text, // Name
                    AccessData.TryToParseInt(lvtItem.SubItems[1].Text), // X
                    AccessData.TryToParseInt(lvtItem.SubItems[2].Text), // Y
                    AccessData.TryToParseInt(lvtItem.SubItems[3].Text), // Z
                    AccessData.TryToParseInt(lvtItem.SubItems[4].Text) // Max Weight
                    );

                foreach (ContainerClass container in Containers) {
                    if (container.Name == containerToDelete.Name &&
                        container.X == containerToDelete.X &&
                        container.Y == containerToDelete.Y &&
                        container.Z == containerToDelete.Z &&
                        container.MaxWeight == containerToDelete.MaxWeight) {
                        Containers.Remove(container);
                        break;
                    }
                }
            }
            AccessData.WriteAllContainers(ProfilesPath, Containers);
            profileDropDwnCmbBox_SelectedIndexChanged(this, null);
        }

        private void itemListView_SelectedIndexChanged(object sender, EventArgs e) {
            deleteContainerBtn.Enabled = false;
            deleteItemBtn.Enabled = (itemListView.SelectedIndices.Count > 0 ? true : false);
        }

        private void containerListView_SelectedIndexChanged(object sender, EventArgs e) {
            deleteItemBtn.Enabled = false;
            deleteContainerBtn.Enabled = (containerListView.SelectedIndices.Count > 0 ? true : false);
        }
    }
}
